package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.controller.JobseekerController;
import sg.edu.nus.iss.ooad.jass.controller.MainController;
import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow; 
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 * Jobseeker's Registration Form.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 * 
 *
 */
public class JASSJobSeekerRegistrationForm extends AbstractJASSRegularWindow {

        private static final long serialVersionUID = -8249586943029528796L;
        //form components
        private Label labelError = null;
        private Label labelInformation = null;
        private Label labelName = null;
        private Label labelAddress = null;
        private Label labelPassword = null;
        private Label labelConfirmPassword = null;
        private Label labelEmail = null;
        private Label labelResume = null;
        private TextField textName = null;
        private TextArea textAddress = null;
        private TextField textPassword = null;
        private TextField textConfirmPassword = null;
        private TextField textEmail = null;
        private TextArea textResume = null;
        private JASSRegularPanel formPanel = null;
        private Panel inputPanel = null;
        private Panel buttonPanel = null;
        private Button btnRegister = null;
        private Button btnCancel = null;
        private Button btnReset = null;
        private GridBagLayout inputLayout = null;

        /**
         * Default Constructor JASSJobseekerRegistrationForm
         */
        public JASSJobSeekerRegistrationForm() {
                super("Job Advertisement and Search System", new Dimension(550, 400));
                initComponents();
        }

        /**
         * Attach the component to the form
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         * @param fill
         */

        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                GridBagConstraints constraint = new GridBagConstraints();
                constraint.gridx = gridx;
                constraint.gridy = gridy;
                constraint.gridwidth = gridwidth;
                constraint.gridheight = gridheight;
                constraint.fill = fill;
                constraint.insets = new Insets(4, 4, 4, 4);
                constraint.weightx = 1.0;
                constraint.weighty = 1.0;
                inputPanel.add(c);
                inputLayout.setConstraints(c, constraint);
        }

        /**
         * This methods will initialize all form components.
         */
        public void initComponents() {
                //initialize components
                labelError = new Label("");
                labelError.setForeground(Color.RED);
                labelInformation = new Label("* means required");
                labelInformation.setForeground(Color.BLUE);
                labelName = new Label("Name");
                labelAddress = new Label("Address");
                labelPassword = new Label("Password");
                labelConfirmPassword = new Label("Confirm Password");
                labelEmail = new Label("Email");
                labelResume = new Label("Resume");
                textName = new TextField(35);
                textAddress = new TextArea(3, 35);
                textPassword = new TextField(25);
                textPassword.setEchoChar('*');
                textConfirmPassword = new TextField(25);
                textConfirmPassword.setEchoChar('*');
                textEmail = new TextField(35);
                textResume = new TextArea(5, 40);
                formPanel = new JASSRegularPanel("Jobseeker Registration");
                inputPanel = new Panel();
                buttonPanel = new Panel();
                btnRegister = new Button("Register");
                btnReset = new Button("Reset");
                btnCancel = new Button("Cancel");

                //add action listener
                btnRegister.addActionListener(new RegisterActionListener(this));
                btnReset.addActionListener(new ResetActionListener());
                btnCancel.addActionListener(new CancelActionListener());

                //setLayout
                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                buttonPanel.add(btnRegister);
                buttonPanel.add(btnReset);
                buttonPanel.add(btnCancel);
                formPanel.add(buttonPanel, BorderLayout.SOUTH);

                inputLayout = new GridBagLayout();
                inputPanel.setLayout(inputLayout);
                attachComponent(labelError, 0, 0, 2, 1);
                attachComponent(labelInformation, 0, 1, 2, 1);
                attachComponent(labelName, 0, 2, 1, 1);
                attachComponent(textName, 1, 2, 1, 1);
                attachComponent(labelEmail, 0, 3, 1, 1);
                attachComponent(textEmail, 1, 3, 1, 1);
                attachComponent(labelPassword, 0, 4, 1, 1);
                attachComponent(textPassword, 1, 4, 1, 1);
                attachComponent(labelConfirmPassword, 0, 5, 1, 1);
                attachComponent(textConfirmPassword, 1, 5, 1, 1);
                attachComponent(labelAddress, 0, 6, 1, 1);
                attachComponent(textAddress, 1, 6, 4, 3);
                attachComponent(labelResume, 0, 9, 1, 1);
                attachComponent(textResume, 1, 9, 4, 5);
                formPanel.add(inputPanel, BorderLayout.CENTER);

                //add to the frame
                add(formPanel);
                pack();
        }

        /**
         * Reset form
         */
        @Override
        public void resetForm() {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Register action handler. It is called when the user click button "Register".
         */
        class RegisterActionListener implements ActionListener {
            private AbstractJASSRegularWindow owner;
           public RegisterActionListener (AbstractJASSRegularWindow owner)
           {
               this.owner =owner;
           }

                public void actionPerformed(ActionEvent e) {

                        //get the user input
                        String name = textName.getText();
                        String email = textEmail.getText();
                        String password = textPassword.getText();
                        String confirmpassword = textConfirmPassword.getText();
                        String address = textAddress.getText();
                        String resume = textResume.getText();

                        //validate user input
                        if (null == name || "".equalsIgnoreCase(name)) {
                                labelError.setText("Name cannot be empty");
                                return;
                        }

                        if (null == email || "".equalsIgnoreCase(email)) {
                                labelError.setText("Email cannot be empty");
                                return;
                        } else {
                                //validate email format
                        }

                        if (null == password || "".equalsIgnoreCase(password)) {
                                labelError.setText("Password cannot be empty");
                                return;
                        } else {
                                if (password.length() < 6) {
                                        labelError.setText("Minimum password length is 6 characters");
                                        return;
                                }
                        }

                        if (null == confirmpassword || "".equalsIgnoreCase(confirmpassword)) {
                                labelError.setText("Password confirmation cannot be empty");
                                return;
                        } else {
                                if (password.length() < 6) {
                                        labelError.setText("Minimum password length is 6 characters");
                                        return;
                                }

                                if (password.compareTo(confirmpassword) != 0) {
                                        labelError.setText("Password and Password Confirmation is not the same");
                                        return;
                                }
                        }  
                        boolean success = false;
                        Jobseeker jobseeker = new Jobseeker("", name, email, password, address, resume, "0");
                        JobseekerController jobseekerController = new JobseekerController();
                        try {
                                success = jobseekerController.createJobseeker(jobseeker);
                              
                                if (success) {
                                       displayMessage(" Job Seeker Registration " ,"Created Successfully");
                                       close();
                                }else
                                {
                                      displayMessage(" Job Seeker Registration "  ,"Failed to create account");
                                }

                                
                              
                        } catch (Exception ex) {
                                Logger.getLogger(JASSJobSeekerRegistrationForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                }
        }

        /**
         * Reset action handler. It is called when the user click button "Reset".
         */
        class ResetActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        textName.setText("");
                        textAddress.setText("");
                        textPassword.setText("");
                        textConfirmPassword.setText("");
                        textEmail.setText("");
                        textResume.setText("");
                }
        }

        /**
         * Cancel action handler. It is called when the user click button "Cancel".
         */
        class CancelActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                     close();
                }
        }

       
}
